package com.jandn.media.action;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Json {
    private boolean isadsT = true;
    
    public boolean isIsadsT() {
        return isadsT;
    }

    public void setIsadsT(boolean isadsT) {
        this.isadsT = isadsT;
    }
    
    public static Object getJson(Object obj) throws JSONException{
        
        //处理Map
        if(obj instanceof Map){
            JSONObject jObj = new JSONObject();
            Map map = (Map)obj;
            Iterator it = map.keySet().iterator();
            for (; it.hasNext();) {
                String key = it.next().toString();
                if(map.get(key) instanceof String){
                    jObj.put(key,map.get(key));
                }else{
                    jObj.put(key,getJson(map.get(key)));
                }
            }
            return jObj;
        }
        
        //处理DTO
        if(obj instanceof ChatDTO){
            JSONObject jObj = new JSONObject();
            Class cla = obj.getClass();
            Field[] f = cla.getDeclaredFields();
            for (int i = 0; i < f.length; i++) {
                Method method = null;
                String key = f[i].getName();
                String upperKey = key.substring(0,1).toUpperCase()+key.substring(1);
                String value = "";
                //可能是get方法
                try {
                    method = cla.getDeclaredMethod("get"+upperKey, null);
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    //没有找到get方法不管继续找is方法
                }
                //可能是is方法
                try {
                    method = cla.getDeclaredMethod("is"+upperKey, null);
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    //没有找到is方法依然不管,这个method现在为null;
                }
                
                try {
                    //如果上面两个都没有找到,说明该字段不存在get或is方法,不是javaBean标准格式.
                    if(method != null){
                        value = String.valueOf(method.invoke(obj, null));
                        jObj.put(key, value);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }

            return jObj;
        }

        //处理List
        if(obj instanceof List){
            JSONArray jList = new JSONArray();
            List list = (List)obj;
            Iterator it = list.iterator();
            for (; it.hasNext();) {
                Object _obj = it.next();
                jList.put(getJson(_obj));
            }
            return jList;
        }
        
        return null;
    }
}
